﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Image_Test1.ObjectTypes;
using Image_Test1.ResizeClasses;

namespace Image_Test1
{
    class ImgAlgebra
    {
        /// <summary>
        /// adds to images
        /// </summary>
        /// <param name="img1">array of pixels of first image</param>
        /// <param name="img2">array of pixels of secobd image</param>
        /// <returns>sum of 2 images</returns>
        public Bitmap addImages(Bitmap Image1, Bitmap Image2, double Range)
        {
            int height = Image1.Height;
            int width = Image1.Width;
            Color[,] Buffer2 = new Color[Image2.Width,Image2.Height];
            for (int i = 0; i < Image2.Width; i++)
                {
                    for (int j = 0; j < Image2.Height; j++)
                    {
                        Buffer2[i, j] = Image2.GetPixel(i, j);
                    }
                }
            Bilinear Bi = new Bilinear();
            Image2 = Bi.ResizeImage(new Size(width, height), Image2, Buffer2);
            int R, G, B;
            Bitmap Result = new Bitmap(Image1.Width, Image1.Height);
            
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    R = (int)(Image1.GetPixel(i, j).R * Range + Image2.GetPixel(i, j).R * (1 - Range));
                    G = (int)(Image1.GetPixel(i, j).G * Range + Image2.GetPixel(i, j).G * (1 - Range));
                    B = (int)(Image1.GetPixel(i, j).B * Range + Image2.GetPixel(i, j).B * (1 - Range));
                    Result.SetPixel(i, j, Color.FromArgb(R, G, B));
                }
            }
            return Result;
        }

        /// <summary>
        /// subs image from another image
        /// </summary>
        /// <param name="img1">array of pixels of first image</param>
        /// <param name="img2">array of pixels of secobd image</param>
        /// <returns>diffrence bettween 2 images</returns>
        /*public Bitmap SubImages(Bitmap Image1, Bitmap Image2)
        {
            //int height = ImageTasks.GetHeight(img1);
            //int width = ImageTasks.GetWidth(img1);
            //img2 = ResizeImage.BilinearResize(img2, height, width);
            //int height2 = ImageTasks.GetHeight(img2);
            //int width2 = ImageTasks.GetWidth(img2);
            //ColorStruct.col[,] result = new ColorStruct.col[height, width];
            int height = Image1.Height;
            int width = Image1.Width;
            Color[,] Buffer2 = new Color[Image2.Width, Image2.Height];
            for (int i = 0; i < Image2.Width; i++)
            {
                for (int j = 0; j < Image2.Height; j++)
                {
                    Buffer2[i, j] = Image2.GetPixel(i, j);
                }
            }
            Bilinear Bi = new Bilinear();
            Image2 = Bi.ResizeImage(new Size(width, height), Image2, Buffer2);
            int R, G, B;
            Image_Test1.MyColor[,] TempColor = new Image_Test1.MyColor[Image1.Width, Image1.Height];
            Bitmap Result = new Bitmap(Image1.Width, Image1.Height);
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    TempColor[i,j].Red = (int)(Image1.GetPixel(i, j).R - Image2.GetPixel(i, j).R);
                    TempColor[i, j].Green = (int)(Image1.GetPixel(i, j).G - Image2.GetPixel(i, j).G);
                    TempColor[i, j].Blue = (int)(Image1.GetPixel(i, j).B - Image2.GetPixel(i, j).B);
                    // result[i, j] = (int)(img1[i, j] + img2[i, j]);
                    Result.SetPixel(i, j, Color.FromArgb(TempColor[i, j].Red, TempColor[i, j].Green, TempColor[i, j].Blue));
                }
            }
            Adjust adj = new Adjust();
            //Result = adj.Contrast(TempColor, 0, 255);
                
            return Result;
        }*/

        public Bitmap SubImages(Bitmap Image1, Bitmap Image2)
        {
            double[,] Red_value = new double[Image1.Width, Image1.Height];
            double[,] Green_value = new double[Image1.Width, Image1.Height];
            double[,] Blue_value = new double[Image1.Width, Image1.Height];

            Color[,] Buffer2 = new Color[Image2.Width, Image2.Height];
            for (int i = 0; i < Image2.Width; i++)
            {
                for (int j = 0; j < Image2.Height; j++)
                {
                    Buffer2[i, j] = Image2.GetPixel(i, j);
                }
            }

            Bitmap new_img = new Bitmap(Image1.Width, Image1.Height);
            Bilinear Bi = new Bilinear();
            Image2 = Bi.ResizeImage(new Size(Image1.Width, Image1.Height), Image2, Buffer2);
            for (int i = 0; i < Image1.Width; i++)
            {
                for (int j = 0; j < Image1.Height; j++)
                {
                    Red_value[i, j] = Image1.GetPixel(i, j).R - Image2.GetPixel(i, j).R;
                    Green_value[i, j] = Image1.GetPixel(i, j).G - Image2.GetPixel(i, j).G;
                    Blue_value[i, j] = Image1.GetPixel(i, j).B - Image2.GetPixel(i, j).B;
                }
            }
            Red_value = Normalization(Red_value,Image1.Width,Image1.Height);
            Green_value = Normalization(Green_value, Image1.Width, Image1.Height);
            Blue_value = Normalization(Blue_value, Image1.Width, Image1.Height);
            for (int i = 0; i < Image1.Width; i++)
            {
                for (int j = 0; j < Image1.Height; j++)
                {
                    new_img.SetPixel(i, j, Color.FromArgb((int)Red_value[i, j], (int)Green_value[i, j], (int)Blue_value[i, j]));

                }
            }

            return new_img;
        }

        /// <summary>
        /// multiply 2 images
        /// </summary>
        /// <param name="img1">array of pixels of first image</param>
        /// <param name="img2">array of pixels of secobd image</param>
        /// <returns>product of 2 images</returns>
        //public static ColorStruct.col[,] ProductImages(ColorStruct.col[,] img1, ColorStruct.col[,] img2)
        //{
        //    int height = ImageTasks.GetHeight(img1);
        //    int width = ImageTasks.GetWidth(img1);
        //    img2 = ResizeImage.BilinearResize(img2, height, width);
        //    ColorStruct.col[,] result = new ColorStruct.col[height, width];
        //    for (int i = 0; i < height; i++)
        //    {
        //        for (int j = 0; j < width; j++)
        //        {
        //            result[i, j].R = (int)((img1[i, j].R * img2[i, j].R) / 255);
        //            result[i, j].G = (int)((img1[i, j].G * img2[i, j].G) / 255);
        //            result[i, j].B = (int)((img1[i, j].B * img2[i, j].B) / 255);
        //            // result[i, j] = (int)(img1[i, j] + img2[i, j]);
        //        }
        //    }
        //    return result;
        //}

        public double[,] Normalization(double[,] norm_arr, int Width, int Height)
        {
            double max = 0;
            double min = 1000;

            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    if (norm_arr[i, j] > max)
                        max = norm_arr[i, j];
                    if (norm_arr[i, j] < min)
                        min = norm_arr[i, j];
                }
            }
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    if (max - min != 0)
                        norm_arr[i, j] = ((norm_arr[i, j] - min) / (max - min)) * 255;
                    else
                        norm_arr[i, j] = 0;
                }
            }
            return norm_arr;


        }
    }
}
